package router

import (
	"crow-k8s/src/config"
	"crow-k8s/src/service"
	"github.com/gin-gonic/gin"
)

// GetPVCList 查看pvc列表
// @Summary 查看pvc列表
// @Description 查看pvc列表
// @Tags PVC
// @Param namespace_name query string true "namespace名字"
// @Success 200 {object} response.Response{data=v1.PersistentVolumeClaimList} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/pvc/list [get]
// @Security Bearer
func GetPVCList(c *gin.Context) {
	namespaceName := c.Query("namespace_name")
	deploymentList, err := service.GetPVCList(config.K8sClientSet, namespaceName)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, deploymentList)
}

// GetPVC 查看指定pvc
// @Summary 查看指定pvc
// @Description 查看指定pvc
// @Tags PVC
// @Param namespace_name query string true "namespace名字"
// @Param pvc_name query string true "pvc名字"
// @Success 200 {object} response.Response{data=v1.PersistentVolumeClaim} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/pvc [get]
// @Security Bearer
func GetPVC(c *gin.Context) {
	namespaceName := c.Query("namespace_name")
	pvcName := c.Query("pvc_name")
	pvcInfo, err := service.GetPVC(config.K8sClientSet, namespaceName, pvcName)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, pvcInfo)
}

// CreatePVC 创建pvc
// @Summary 创建pvc
// @Description 创建pvc
// @Tags PVC
// @Accept  application/json
// @Product application/json
// @Param data body service.CreatePVCRequest true "创建PVC的信息"
// @Success 200 {object} response.Response{data=v1.PersistentVolumeClaim} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/pvc [post]
// @Security Bearer
func CreatePVC(c *gin.Context) {
	var request service.CreatePVCRequest
	err := c.Bind(&request)
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	pvcInfo, err := service.CreatePVC(config.K8sClientSet, request)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, pvcInfo)
}

// DeletePVC 删除指定pvc
// @Summary 删除指定pvc
// @Description 删除指定pvc
// @Tags PVC
// @Accept  application/json
// @Product application/json
// @Param data body service.DeletePVCRequest true "删除PVC所需信息"
// @Success 200 {object} response.Response{data=string} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/pvc [delete]
// @Security Bearer
func DeletePVC(c *gin.Context) {
	var request service.DeletePVCRequest
	err := c.Bind(&request)
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	err = service.DeletePvc(config.K8sClientSet, request)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, "删除成功")
}
